package com.lang8.user.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.sql.Types;
import java.util.Calendar;
import java.util.Date;

import com.lang8.user.constants.UserConstants;
import com.lang8.user.po.UserInfo;
import com.lang8.util.CodeUtil;
import com.lang8.util.database.ConnectionUtil;

public class UserDao {

	public void insertUser(UserInfo userInfo) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			conn = ConnectionUtil.getConnection();
			conn.setAutoCommit(false);
			String sql = "INSERT INTO user_info(LOGIN_ID,PASSWORD,NICK_NAME,PIC_STATUS,NATIVE_LANG,LEARNING_LANG,USER_STATUS,CREATE_TIME,CREATE_IP,LAST_LOGIN_TIME,LAST_LOGIN_IP,LAST_MODIFY_TIME,LAST_MODIFY_IP)VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)";
			pstmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
			//LOGIN_ID
			pstmt.setString(1, userInfo.getLoginId());
			//PASSWORD
			pstmt.setString(2, userInfo.getPassword());
			//NICK_NAME
			pstmt.setString(3, userInfo.getNickName());
			//PIC_STATUS
			pstmt.setInt(4, UserInfo.PIC_STATUS_NO);
			//NATIVE_LANG
			pstmt.setString(5, userInfo.getNativeLang());
			//LEARNING_LANG
			pstmt.setString(6, userInfo.getLearningLang());
			//USER_STATUS
			pstmt.setInt(7, UserInfo.USER_STATUS_NEW);
			//CREATE_TIME
			pstmt.setTimestamp(8, new Timestamp(userInfo.getCreateTime().getTime()));
			//CREATE_IP
			pstmt.setString(9, userInfo.getCreateIp());
			//LAST_LOGIN_TIME
			pstmt.setTimestamp(10, new Timestamp(userInfo.getLastLoginTime().getTime()));
			//LAST_LOGIN_IP
			pstmt.setString(11, userInfo.getLastLoginIp());
			//LAST_MODIFY_TIME
			pstmt.setTimestamp(12, new Timestamp(userInfo.getLastModifyTime().getTime()));
			//LAST_MODIFY_IP
			pstmt.setString(13, userInfo.getLastModifyIp());
			pstmt.executeUpdate();
			rs = pstmt.getGeneratedKeys();
			if (rs.next()) {
				userInfo.setUserInfoId(rs.getLong(1));
			}
			conn.commit();
		} catch (Exception e) {
			if (conn != null) {
				try {
					conn.rollback();
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}
			e.printStackTrace();
		} finally {
			ConnectionUtil.releaseConnection(rs, pstmt, conn);
		}
	}

	public String insertRegisterEmail(UserInfo userInfo) throws SQLException {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			conn = ConnectionUtil.getConnection();
			String sql = "INSERT INTO user_email_activate (USER_EMAIL_ACTIVATE_ID, USER_INFO_ID, ACTIVATE_STATUS, SENT_TIME, EXPIRE_TIME, RESENT_COUNT, ACTIVATE_TIME) VALUES (?,?,?,?,?,?,?)";
			pstmt = conn.prepareStatement(sql);
			String uuid = CodeUtil.getUUID();
			//1. USER_EMAIL_ACTIVATE_ID
			pstmt.setString(1, uuid);
			//2. USER_INFO_ID
			pstmt.setLong(2, userInfo.getUserInfoId());
			//3. ACTIVATE_STATUS
			pstmt.setInt(3, UserConstants.USER_EMAIL_ACTIVATE_STATUS_SENT);
			//4. SENT_TIME
			Date sentTime = new Date();
			pstmt.setTimestamp(4, new Timestamp(sentTime.getTime()));
			//5. EXPIRE_TIME
			Calendar c = Calendar.getInstance();
			c.setTime(sentTime);
			c.add(Calendar.HOUR_OF_DAY, 48);
			pstmt.setTimestamp(5, new Timestamp(c.getTime().getTime()));
			//6. RESENT_COUNT
			pstmt.setInt(6, 0);
			//7. ACTIVATE_TIME
			pstmt.setNull(7, Types.TIMESTAMP);
			pstmt.executeUpdate();
			return uuid;
		} finally {
			ConnectionUtil.releaseConnection(rs, pstmt, conn);
		}
	}

}
